package com.tiankong.product.controller;


import com.tiankong.product.dto.CategoryDrag;
import com.tiankong.product.entity.Category;
import com.tiankong.product.service.CategoryService;
import net.youdi.common.model.Result;
import net.youdi.common.util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping("list/tree")
    public Result tree(){
        List<Category> list = categoryService.query().orderByAsc("sort","cat_id").list();
        Object tree = TreeUtil.packageTree(list, 0);
        return Result.ok(tree);
    }

    @PostMapping("save")
    public Result save(@RequestBody Category category){
        categoryService.saveCategory(category);
        return Result.ok(category);
    }

    @GetMapping("info/{id}")
    public Result info(@PathVariable("id") Long id){
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    @DeleteMapping("delete")
    public Result delete(@RequestBody Set<Long> ids){
        categoryService.batchDelete(ids);
        return Result.ok();
    }

    @PostMapping(value = "drag",name = "拖拽保存节点")
    public Result drag(@RequestBody CategoryDrag categoryDrag){
        categoryService.drag(categoryDrag);
        return Result.ok();
    }
    @PostMapping(value = "update/batch",name = "批量更新节点")
    public Result batchUpdate(@RequestBody List<Category> categories){
        categoryService.updateBatchById(categories);
        return Result.ok();
    }




}
